/**
 * 
 */
package org.iridium.allcomments.persistance;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author iridium
 * 
 */
public class UserTagDAOImpl extends AbstractSystemDAO implements UserTagDAO {
	
	@Override
	public boolean existsTag(long userId, String tag) {
		this.buildConnection();
		String sql = "select * from user_tag where user_id = ? and tag = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		long tagQry = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setString(2, tag);
			rs = ps.executeQuery();
			while (rs.next()) {
				tagQry = rs.getLong(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		this.closeConnection();
		if (userId == tagQry) {
			return true;
		}
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.UserTagDAO#addItem(java.lang.String,
	 * java.lang.String, int)
	 */
	@Override
	public void addItem(long userId, String tag, int visibility) {
		this.buildConnection();
		String sql = "insert into user_tag(user_id, tag, visibility) values(?, ?, ?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setString(2, tag);
			ps.setInt(3, visibility);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		this.closeConnection();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.UserTagDAO#delItem(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public void delItem(long userId, String tag) {
		this.buildConnection();
		String sql = "delete from user_tag where user_id = ? and tag = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.setString(2, tag);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

	@Override
	public void deleteTag(long userId) {
		this.buildConnection();
		String sql = "delete from user_tag where user_id = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, userId);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.UserTagDAO#deleteTag(java.lang.String
	 * , java.lang.String)
	 */
	@Override
	public void deleteTag(long userId, String tag) {
		this.delItem(userId, tag);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.iridium.allcomments.persistance.UserTagDAO#updateVisibility(java.
	 * lang.String, java.lang.String, int)
	 */
	@Override
	public void updateVisibility(long userId, String tag, int visibility) {
		this.buildConnection();
		String sql = "update user_tag set visibility = ? where user_id = ? and tag = ?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, visibility);
			ps.setLong(2, userId);
			ps.setString(3, tag);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.closeConnection();
	}

}
